//
//  LYTeacherViewController.m
//  artist
//
//  Created by Jay_Apple on 16/4/12.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYTeacherViewController.h"
#import "LYScreenBar.h"
#import "LYChooseListView.h"
#import "LYSubjectListDBTool.h"
#import "LYTeacherCell.h"
#import "LYTeacherWebViewController.h"
#import "VVLookatMapController.h"
#import "LYCityViewController.h"

@interface LYTeacherViewController ()<VVYRefreshTableViewDelegate, UITableViewDataSource>


@property (nonatomic, weak) LYChooseListView *chooseView;

@property (nonatomic, strong) NSMutableArray *models;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic) NSInteger page;

@property (weak, nonatomic) LYScreenBar *screenBar;

@end

@implementation LYTeacherViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //初始化导航栏
    [self setUpNav];
    
    //初始化筛选栏
    [self setUpScreenBar];
    
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    LYLocationInfo *locationInfo = [LYDataSourceManager sharedManager].locationInfo;
    
    NSInteger subID = -1;
    
    if (self.screenBar.subTitle) {
        subID = [LYSubjectListDBTool getIDWithSubTitle:self.screenBar.subTitle];
    }
    
    LYConditionModel *teacherModel = [[LYConditionModel alloc] init];
    
    teacherModel.area_id = [NSString stringWithFormat:@"%ld",(long)self.screenBar.area_id];
    
    teacherModel.page = @"1";
    teacherModel.per_page = @"10";
    teacherModel.distinct = @"10";
    teacherModel.role_id = LYRoleTypeTeacher;
    teacherModel.token = LYToken;
    teacherModel.order_type = self.screenBar.orderType;
    teacherModel.subject_path = subID == -1 ? nil : [NSString stringWithFormat:@"%ld",(long)subID];
    
    if (locationInfo) {
        teacherModel.lat = locationInfo.lat;
        teacherModel.lng = locationInfo.lng;
    }
    
    [LYRequestTool getLocalTeacherAndOrganizationWithcontionModel:teacherModel success:^(NSArray<LYPersonalInfo *> *models) {
        
        [self.tableView.mj_header endRefreshing];
         [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;

        
    } failure:^(NSString *code, NSString *message) {
        
         [self.tableView.mj_header endRefreshing];
    }];
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
     LYLocationInfo *locationInfo = [LYDataSourceManager sharedManager].locationInfo;
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    NSString *perpage = @"10";

    NSInteger subID = -1;
    
    if (self.screenBar.subTitle) {
        subID = [LYSubjectListDBTool getIDWithSubTitle:self.screenBar.subTitle];
    }
    
    LYConditionModel *teacherModel = [[LYConditionModel alloc] init];
    
    teacherModel.page = page;
    teacherModel.per_page = perpage;
    teacherModel.distinct = @"10";
    teacherModel.role_id = LYRoleTypeTeacher;
    teacherModel.token = LYToken;
    teacherModel.order_type = self.screenBar.orderType;
    teacherModel.subject_path = subID == -1 ? nil : [NSString stringWithFormat:@"%zd",subID];
    
    if (locationInfo) {
        teacherModel.lat = locationInfo.lat;
        teacherModel.lng = locationInfo.lng;
    }

    
    [LYRequestTool getLocalTeacherAndOrganizationWithcontionModel:teacherModel success:^(NSArray<LYPersonalInfo *> *models) {
        
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
        
    } failure:^(NSString *code, NSString *message) {
        
        [self.tableView.mj_footer endRefreshing];
    }];
    
    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}


#pragma mark - 初始化
/**
 *  初始化筛选栏
 */
- (void)setUpScreenBar{
    
    LYScreenBar *screenBar = [LYScreenBar viewForNib];
    
    screenBar.frame = CGRectMake(0, 0, LYScreenWidth, 40);
    self.screenBar = screenBar;
    
    __weak typeof(self) weakSelf = self;
    __weak typeof(screenBar) weakScreenBar = screenBar;
    [self.view addSubview:screenBar];
    
    screenBar.block = ^(SceenType type){
        
        switch (type) {
            case SceenTypeSub:
                
                if (weakSelf.chooseView) {
                    [weakSelf.chooseView dismiss];
                }else{
                    
                    weakSelf.chooseView = [LYChooseListView showOnView:weakSelf.tableView];
                    weakSelf.chooseView.block = ^(NSString *title){
                        if (title) {
                            weakScreenBar.subTitle = title;
                            [weakSelf.tableView.mj_header beginRefreshing];
                        }
//                        [weakScreenBar backToNormal];
                    };
                }
                break;
                
            case SceenTypeEva:
            {
                [weakSelf.chooseView dismiss];
                [self showPickImageAlertView];
            }
                break;
               
            case SceenTypeDistance:
            {
                [weakSelf.chooseView dismiss];
                
                LYCityViewController *cityVC = [[LYCityViewController alloc] init];
                
                cityVC.block = ^(NSString *cityName, NSInteger cityID){
                    
                    weakScreenBar.nearByTitle = cityName;
                    weakScreenBar.area_id = cityID;
                    [weakSelf.tableView.mj_header beginRefreshing];
                    
                    
                    
                    
#warning ??????????
                };
                
                [self.navigationController pushViewController:cityVC animated:YES];
            }
                break;
                
            default:
                break;
        }
    };
    
}



/**
 *  初始化导航栏
 */
- (void)setUpNav{
    
    self.navigationItem.title = @"周边老师";
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"地图"] style:UIBarButtonItemStyleDone target:self action:@selector(checkMap)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark - tableviewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYTeacherCell";
    
    LYTeacherCell *cell = [LYTeacherCell cellWithTableView:tableView andIndetifier:cellID];
    
    LYPersonalInfo *model = self.models[indexPath.section];
    cell.model = model;
    
    return cell;
}

#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.1;
    }else{
        return 5;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYPersonalInfo *info = self.models[indexPath.section];
    LYTeacherWebViewController *teaWebVC = [LYTeacherWebViewController teaWebVCWithID:info.user_id];
    [self.navigationController pushViewController:teaWebVC animated:YES];

}

#pragma mark - 按钮点击
- (void)checkMap{

    VVLookatMapController *mapView = [[VVLookatMapController alloc] init];
    mapView.models = self.models;
    [self.navigationController pushViewController:mapView animated:YES];

}




-(void)showPickImageAlertView{
    
    __weak typeof(self) weakSelf = self;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *sortAction = [UIAlertAction actionWithTitle:@"智能排序" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"智能排序" ordertype:LYOrderTypeDefault];
    }];
    
    UIAlertAction *renqiAction = [UIAlertAction actionWithTitle:@"人气最高" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"人气最高" ordertype:LYOrderTypeRenqi];
    }];
    
    UIAlertAction *evaluateAction = [UIAlertAction actionWithTitle:@"评价最高" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"评价最高" ordertype:LYOrderTypeEvaluate];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    
    
    [alertController addAction:sortAction];
    [alertController addAction:renqiAction];
    [alertController addAction:evaluateAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}


- (void)alertActionWithTitle:(NSString *)title ordertype:(LYOrderType)orderType {
    
    self.screenBar.sortTitle = title;
    self.screenBar.orderType = orderType;
    [self.tableView.mj_header beginRefreshing];
}

@end
